from typing import Dict
from PySide6.QtWidgets import QTreeWidgetItem
from .TreeWidgetItemABC import TreeWidgetItemABC

class TreeWidgetItemCanMessage(TreeWidgetItemABC):
    '''
    description:
        用于展示 CANMessage 的 TreeWidgetItem 对象
    CANMessage字典结构:
    '''
    def __init__(self, d_data: 'dict' = dict()) -> None:
        super().__init__(d_data=d_data)
        
    
    def Refresh(self, d_data: 'dict') -> None:
        # return super().Refresh(d_data)
        self.setText(0, str(d_data['time']))
        self.setText(1, str(d_data['channel']))
        self.setText(2, str(d_data['id']))
        self.setText(3, d_data['name'])
        self.setText(4, d_data['eventType'])
        self.setText(5, d_data['direction'])
        self.setText(6, str(d_data['dlc']))
        self.setText(7, d_data['data'].hex())
        children_ = d_data.get('children', {})
        if (len(children_) > 0):
            type_ = d_data['children']['type']
            if (type_ == 'pdu'):
                for pdu_ in d_data['children']['items']:
                    item_ = QTreeWidgetItem()
                    item_.setText(0, pdu_['pduName'])
                    item_.setText(1, str(pdu_['data']))
                    self.addChild(item_)
            elif (type_ == 'signal'):
                for signal_ in d_data['children']['items']:
                    item_ = QTreeWidgetItem()
                    item_.setText(0, signal_['signalName'])
                    item_.setText(1, signal_['signalValue'].hex())
                    item_.setText(2, signal_['mean'])
                    self.addChild(item_)

